package scc.com.shuiyin;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

public class MainActivity extends AppCompatActivity {

    private String TAG = getClass().getSimpleName();
    private ImageView iv;
    private ImageView iv2;
    private ImageView iv3;
    private Bitmap aa;
    private Bitmap bb;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
        iv2 = (ImageView) findViewById(R.id.iv2);
        iv3 = (ImageView) findViewById(R.id.iv3);
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    String str = "*************";
    String str2 = "oooo";
    byte[] yuvData1;//图片1
    byte[] yuvData2;//图片2
    byte[] yuvData3;//合并图片

    public void change(View view) {

        Log.d(TAG, "开始: " + System.currentTimeMillis());


        aa = BitMapUtils.getBitmapByString(str, Color.YELLOW, 80);
        bb = BitMapUtils.getBitmapByString(str2, Color.YELLOW, 80);

        yuvData1 = new YuvUitls().getNv21ByString(str, Color.YELLOW, 80);
        yuvData2 = new YuvUitls().getNv21ByString(str2, Color.WHITE, 80);

        //将yuvdata 转化为 图片
        Bitmap bitmap1 = new YuvUitls().rawByteArray2RGBABitmap2(yuvData1, aa.getWidth(), aa.getHeight());
        iv.setImageBitmap(bitmap1);
        Bitmap bitmap2 = new YuvUitls().rawByteArray2RGBABitmap2(yuvData2, bb.getWidth(), bb.getHeight());
        iv2.setImageBitmap(bitmap2);
        Log.d(TAG, "结束: " + System.currentTimeMillis());
    }

    /**
     * 合并yuv 生成图片
     *
     * @param view
     */
    public void combine(View view) {
        long currentTimeMillis = System.currentTimeMillis();
        Log.d(TAG, "开始: " + System.currentTimeMillis());

        //获取yuvdata1的y数据
        byte[] yuvData1Y = subBytes(yuvData1, 0, (int) (yuvData1.length * (2 * 1.0f / 3)));
        //获取yuvData2的y数据
        byte[] yuvData2Y = subBytes(yuvData2, 0, (int) (yuvData2.length * (2 * 1.0f / 3)));
        Log.d(TAG, "combine: " + yuvData1Y.length);
        Log.d(TAG, "combine: " + yuvData2Y.length);


        //获取小图片的宽度
        int width2 = bb.getWidth();
        //获取打图片的宽度
        int width1 = aa.getWidth();


        //合并y的数据
        //
        for (int i = 0; i < yuvData2Y.length; i++) {
            //获取应在大图片的第几行
            int h = i / width2;
            int j = i % width2;
            yuvData1[h * width1 + j] = yuvData2[i];


        }
//        int uvLength = (int) (yuvData2.length*(1.0f/3));
//        for(int i = 0 ; i<uvLength;i++){
//            int i1 = (int) (yuvData1.length * ((2 * 1.0f) / 3));
//            int h = (i1+i) / width2;
//            int j = (i1+i) % width2;
//
//            yuvData1[(int) (yuvData1.length*((2*1.0f)/3))+h * width1 + j] =
//                    yuvData2[(int) (yuvData1.length*((2*1.0f)/3))+i];
//        }



        //合并uv数据
        //计算小图片uv的开始位置
        int postionU2 = (int) (yuvData2.length * ((2 * 1.0f) / 3)) + 1;



        long currentTimeMillis1 = System.currentTimeMillis();
        Log.d(TAG, "结束: " + System.currentTimeMillis());
        Log.d(TAG, "总共耗时: " + (currentTimeMillis1 - currentTimeMillis));
        //合并u的数据
//
//        //先获取yuvData1 u数据的开始位置
//        int postionU1 = (int) (yuvData1.length * ((2 * 1.0f) / 3)) + 1;
//        //获取yuvData2 u数据的开始位置
//        int postionU2 = (int) (yuvData2.length * ((2 * 1.0f) / 3)) + 1;
//
//        //计算u数据的长度
//        int uLength = (int) (yuvData2.length * ((1 * 1.0f) / 4));

//        //替换 (uv数据交替出现，+2）
//        for(int i = 0;i<uLength;i+=2){
//            yuvData1[postionU1+i] = yuvData2[postionU2+i];
////            yuvData1[postionU1+i+1] = yuvData2[postionU2+i+1];
//
//        }


        //替换v的数据


        Bitmap bitmap3 = new YuvUitls().rawByteArray2RGBABitmap2(yuvData1, aa.getWidth(), bb.getHeight());
        iv3.setImageBitmap(bitmap3);


    }

    private byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        System.arraycopy(src, begin, bs, 0, count);
        return bs;
    }

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("Main Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }
}
